﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// StaticDataSourceManageWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StaticDataSourceManageWindow : Window
    {
        public StaticDataSourceManageWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitStaticDataSourceList();
        }

        private void InitStaticDataSourceList()
        {
            Metadata.StaticDataSource[] staticdataSources = Coolzon.Designtime.ResPoolFactory.GetDataSourcePool().GetStaticDataSources();
            if (staticdataSources != null)
            {
                _ctlLV.ItemsSource = staticdataSources;
            }
        }

        private void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditStaticDataSource();
        }

        private void EditStaticDataSource()
        {
            if(_ctlLV.SelectedItem != null)
            {
                Metadata.StaticDataSource selectedStaticDataSource = _ctlLV.SelectedItem as Metadata.StaticDataSource;

                Designtime.Metadata.StaticDataSource staticDataSource = Designtime.Metadata.SolutionHelper.GetStaticDataSource(UIContext.Current.CurrentSolution, selectedStaticDataSource.Name);
                string filePath = Designtime.Metadata.LocalStorage.GetStaticDataSourceAbsolutePath(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID, staticDataSource.Id);
                StaticDataSourceWindow window = new StaticDataSourceWindow(filePath, staticDataSource.Id);
                if (window.ShowDialog().Value)
                {
                    InitStaticDataSourceList();
                }
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StaticDataSourceWindow window = new StaticDataSourceWindow();
            if (window.ShowDialog().Value)
            {
                InitStaticDataSourceList();
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _ctlEdit_Click(object sender, RoutedEventArgs e)
        {
            EditStaticDataSource();
        }
    }
}
